<?php
class Custom_Model_Db	extends	Zend_Db_Table
{
	public function __construct()
	{
		$this->dbprefix=Zend_Registry::get('dbprefix');
		//$this->CoreCache=Zend_Registry::get('coreCache');
		$this->_name=$this->dbprefix.$this->_name;
		
		parent::__construct();
	}
	
	
	//根据条件统计表内总数据
	public function	getTotal($where=1){
        try {
        	$db = $this->getAdapter();
        	$select = $db->select();
			$select->from($this->_name, 'count(*) as total')
					->where($where);
			$sql = $select->__toString();//生成SQL语句
			$nums = $db->query($sql)->fetchColumn();
			
        	return $nums;
        } catch (Exception $e) {
			return false;
        }
	}

	/**
	 * 通到数据表列名，取到总数据
	 * @param unknown_type $col
	 * @param unknown_type $where
	 * @return Ambigous <string, boolean, mixed>|boolean
	 */
	public function getSum($col,$where=1){
		try {
			$this->db = $this->getAdapter();
			$select = $this->db->select();
			$select->from($this->_name, 'sum('.$col.') as total')
			->where($where);
			$sql = $select->__toString();//生成SQL语句
			$nums = $this->db->query($sql)->fetchColumn();
			if(empty($nums))$nums=0;
			return $nums;
		} catch (Exception $e) {
			return false;
		}
	}
	
	
	/**
	 * 取到一条数据内容,默认type为0,返回一维数组,否则为二维数组
	 * Enter description here ...
	 * @param int $id
	 * @param int $type
	 */
	public function getOneById($id,$type=0){
		try {
			$where = $this->_primary."  =$id";
			$condition = $this->select()->where($where);
			if($type){
				$result = $this->fetchAll($condition)->toArray();
			}else{
				$result = $this->fetchRow($condition)->toArray();
			}
        	return $result;
        } catch (Exception $e) {
			return false;
        }
	}
	
 	/**
     * 增加一条数据
     * @param array $data
     */
	public function add(array $data){
		try {
            $result = $this->createRow($data);
        	$id=$result->save();
        	return $id;
        } catch (Exception $e) {
        	var_dump($e);
			return false;
        }
	}
	
	/**
	 * 修改对应数据
	 * @param array $data
	 * @param int $id
	 */
	public function edit(array $data,$where){
		try {
			$condition = $this->select()->where($where);	
			$result = $this->fetchRow($condition);
			$result->setFromArray($data);       
			$id=$result->save();
        	return $id;
        } catch (Exception $e) {
			return false;
        }
	}
	
	
    /**
     * 通过ID数组进行,批量修改
     * @param array $data
     * @param array $ids
     */
	public function	editByArray(array $data,array $ids){
	
		$string = join ( ",", $ids );
        $where = $this->_primary."  IN ( $string )";
        try {
            $result=$this->update($data,$where);
        	return true;
        } catch (Exception $e) {
			return false;
        }
	}
    
	
	/**
	 * 批量删除
	 * @param array $ids
	 */
	public function	delByArray(array $ids){
	
		$string = join ( ",", $ids );
        $where = $this->_primary."  IN ( $string )";
        try {
        	$this->delete($where);
        	return true;
        } catch (Exception $e) {
			return false;
        }
	}
	
	/**
	 * 删除单条数据
	 * Enter description here ...
	 * @param unknown_type $id
	 */
	public function	delByID($id){
		
        $where = $this->_primary."  =$id";
        try {
        	$this->delete($where);
        	return true;
        } catch (Exception $e) {
			return false;
        }
	}


	
}
